//app.js
App({
  onLaunch: function () {

    var that = this
    var storageData = wx.getStorageSync('myId')
    if (storageData != '' && storageData != undefined && storageData!=null){
      this.globalData.myId = storageData
    }
    const vm = this
    wx.getSystemInfo({
      success: function (res) {
        let totalTopHeight = 68
        if (res.model.indexOf('iPhone X') !== -1) {
          totalTopHeight = 88
        } else if (res.model.indexOf('iPhone') !== -1) {
          totalTopHeight = 64
        }
        vm.globalData.statusBarHeight = res.statusBarHeight
        vm.globalData.titleBarHeight = totalTopHeight - res.statusBarHeight
      },
      failure() {
        vm.globalData.statusBarHeight = 0
        vm.globalData.titleBarHeight = 0
      }
    })

  },  
  reLogin: function () {
    wx.showModal({
      title: '提示',
      content: '账号失效，请重新登录',
      showCancel: false,
        success: function (res) {
        if (res.confirm) {
        
        } 
      }
    })
  },

  get3rd: function (callback) {

    var that = this
    wx.checkSession({
      success: function (res) {
        var storageData = wx.getStorageSync('session3rd')
        if (storageData != "") {
          if (callback != null && callback != '')
            callback(storageData)
        }
        else {
          // that.startFromLogin(callback);
          callback(null)
        }

      },
      fail: function () {
        // that.startFromLogin(callback);
      }
    })

  },
  get3rdStr: function () {
    return wx.getStorageSync('session3rd');
  },

  startFromLogin: function (callback) {
    var that = this
    wx.login({
      success: function (res) {
        var code = res.code;
        wx.getUserInfo({
          success: function (res) {
            that.login3rdServer(code, res, callback)
          }, fail: function () {
            that.reauth(callback)
          }
        })
      },
      fail: function () {
      }
    })
  },

  reauth: function (callback) {
    var that = this
    wx.openSetting({
      success: function (data) {
        if (data.authSetting["scope.userInfo"] == true) {
          wx.login({
            success: function (res) {
              var code = res.code;
              wx.getUserInfo({
                success: function (res) {
                  that.login3rdServer(code, res, callback)
                }
              })
            },
            fail: function () {
            }
          })
        } else {        
          wx.hideLoading()
        }
      },
      fail: function () {
      }
    });
  },

  login3rdServer: function (code, res, callback) {
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/Login/index',
      data: {
        code: code,
        rawData: res.rawData,
        encryptedData: res.encryptedData,
        signature: res.signature,
        iv: res.iv,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (ress) {   
        that.globalData.binding = ress.data.binding
        wx.setStorageSync('binding', ress.data.binding)
        if (ress.data.session3rd != null && ress.data.session3rd!=''){
          wx.setStorageSync('session3rd', ress.data.session3rd)
        }else{
        }
        if (callback != null && callback != '')
          callback(ress.data.session3rd)

      }
    })
  },
  
  
  globalData: {

    isLoginPage:false,
    userInfo: null,
    myId:'',
    member: '',
    num:'',

  }
})
